﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using BudgetApp.Data;
using BudgetApp.Data.Models;

namespace BudgetApp.Adapters
{
    public class BudgetDataAdapter : IBudgetAdapter
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        public TrackSpend GetSpendDetails(int id)
        {
            TrackSpend trackSpend = db.TrackSpends.Find(id);
            return trackSpend;
        }

        public TrackBill GetBillDetails(int id)
        {
            TrackBill trackBill = db.TrackBills.Find(id);
            return trackBill;
        }

        public Category GetCategoryDetails(int id)
        {
            Category category = db.Categories.Find(id);
            return category;
        }

        public SubCategory GetSubCategoryDetails(int id)
        {
            SubCategory subCategory = db.SubCategories.Find(id);
            return subCategory;
        }

        public Dictionary<string, int> IncomeTotals()
        {
            Dictionary<string, int> model = new Dictionary<string, int>();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var budget = db.TrackBills.ToList();
                decimal income = budget.Where( i => i.InputType.Type == "Income").Sum(x => x.Amount);
                decimal bills = budget.Where(i => i.InputType.Type == "Bills").Sum(x => x.Amount);

                decimal num = income;
                int n = Convert.ToInt32(num);

                decimal mun = bills;
                int m = Convert.ToInt32(mun);

                model.Add("Income", n);
                model.Add("Bills", m);
            }

            return model;
        }

        public ApplicationUser GetUserById(string userId)
        {
            ApplicationUser model = new ApplicationUser();
            if (userId != null)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    ApplicationUser user = db.Users.FirstOrDefault(u => u.Id == userId);
                    model.FirstName = user.FirstName;
                    model.LastName = user.LastName;
                    model.Email = user.Email;
                    model.UserName = user.UserName;
                }
            }
            return model;
        }
    }

    public class Totals
    {
        public string Label { get; set; }
        public decimal Value { get; set; }
    }
}